package com.ouyang.filter;

import com.ouyang.poji.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginFilter implements Filter {
    @Override
    public void init ( FilterConfig filterConfig ) throws ServletException {

    }

    @Override
    public void doFilter ( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain ) throws IOException, ServletException {
        HttpServletRequest rq = (HttpServletRequest)servletRequest;
        HttpServletResponse rp = (HttpServletResponse)servletResponse;
        User userSession = (User)rq.getSession().getAttribute("usersession");
        if(userSession==null){
            rp.sendRedirect(rq.getContextPath()+"/error.jsp");
        }else{
            filterChain.doFilter(servletRequest, servletResponse);
        }

    }

    @Override
    public void destroy ( ) {

    }
}
